<?php
require_once dirname(__FILE__) . "/../config/functions.php";
$fullUrl = full_url();
include_once dirname(__FILE__) . '/header.php';

//check for delete
$deleteFlagBanner = FALSE;
$deleteFlagLink = FALSE;
if (isset($_GET['action']) && $_GET['action'] == 'deleteBanner') {
    if (isset($_GET['id']) && $_GET['id']) {
        mysql_query("delete from `option` WHERE id = " . $_GET['id']);
        $deleteFlagBanner = TRUE;
    }
}
if (isset($_GET['action']) && $_GET['action'] == 'deleteLink') {
    if (isset($_GET['id']) && $_GET['id']) {
        mysql_query("delete from `option` WHERE id = " . $_GET['id']);
        $deleteFlagLink = TRUE;
    }
}

//add new Link
if (isset($_POST) && $_POST && isset($_POST['hits'])) {
    //check for errors
    $errorsLink = array();
    $errorsBanner = array();
    if (!$_POST['name']) {
        $errorsLink[] = 'You must add Name.';
    }
    if (!$_POST['hits']) {
        $errorsLink[] = 'You must add Hits.';
    }
    if (!$_POST['cost']) {
        $errorsLink[] = 'You must add Cost.';
    }
    if (count($errorsLink) > 0) {
        
    } else {
        $name = $_POST['name'];
        $hits = $_POST['hits'];
        $cost = $_POST['cost'];
        mysql_query("insert into `option` (`key`, `value`) values ('link_price_$name','$hits^$cost') ");
        $deleteFlagBanner = FALSE;
        $deleteFlagLink = FALSE;
    }
}
//add new banner
if (isset($_POST) && $_POST && isset($_POST['views'])) {
    //check for errors
    $errorsBanner = array();
    $errorsLink = array();
    if (!$_POST['name']) {
        $errorsBanner[] = 'You must add Name.';
    }
    if (!$_POST['views']) {
        $errorsBanner[] = 'You must add Impressions.';
    }
    if (!$_POST['cost']) {
        $errorsBanner[] = 'You must add Cost.';
    }
    if (count($errorsBanner) > 0) {
        
    } else {
        $name = $_POST['name'];
        $views = $_POST['views'];
        $cost = $_POST['cost'];
        mysql_query("insert into `option` (`key`, `value`) values ('banner_price_$name','$views^$cost') ");
        $deleteFlagBanner = FALSE;
        $deleteFlagLink = FALSE;
    }
}

$query = "SELECT * from `option` where `key` LIKE '%link_price_%'";
$adLinks = mysql_query($query);
$query = "SELECT * from `option` where `key` LIKE '%banner_price_%'";
$adBanners = mysql_query($query);

?>

<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1>Advertise Links <small>set costs</small></h1>

            <?php if ($deleteFlagLink): ?>
                <div class="alert alert-success">
                    Record Deleted Successfully.
                </div>
            <?php endif; ?>

            <a href="javascript:void(0)" onclick="$('.addNewLink').slideToggle();" class="addNewLinkBtn btn btn-default">Add New</a>

            <?php
            //add new
            if (isset($_POST) && $_POST && isset($_POST['hits'])) {
                if (count($errorsLink) > 0) {
                    foreach ($errorsLink as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }
                } else {
                    echo '<div class="alert alert-success">Record Added Successfully.</div>';
                }
            }
            ?>

            <div class="addNewLink">
                <form class="form-horizontal" role="form" method="post">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" id="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="hits" class="col-sm-2 control-label">Hits</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="hits" id="hits">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cost" class="col-sm-2 control-label">Cost</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="cost" id="cost">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-primary">Add</button>
                        </div>
                    </div>
                </form>
            </div>

            <table class="table table-bordered">
                <thead><tr><th>#</th><th>Name </th><th>Hits</th><th>Cost ($)</th><th>Action</th></tr></thead>
                <tbody>
                    <?php
                    $index = 1;
                    while ($row = mysql_fetch_array($adLinks)) :
                        ?>
                        <tr>
                            <td><?php echo $index; ?></td>
                            <td><?php $name = explode("link_price_", $row['key']); echo $name[1]; ?></td>
                            <td><?php $value = explode("^", $row['value']); echo $value[0]; ?></td>
                            <td><?php echo $value[1]; ?></td>
                            <td>
                                <a href="advertising.php?action=deleteLink&id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure you want to remove this record.');"><span class="glyphicon glyphicon-remove"></span></a>
                            </td>
                        </tr>
                        <?php
                        $index++;
                    endwhile;
                    ?>
                </tbody>
            </table>

          
        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->

<!--Banner-->

<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1>Banners Impressions <small>set costs</small></h1>

            <?php if ($deleteFlagBanner): ?>
                <div class="alert alert-success">
                    Record Deleted Successfully.
                </div>
            <?php endif; ?>

            <a href="javascript:void(0)" onclick="$('.addNewBanner').slideToggle();" class="addNewBannerBtn btn btn-default">Add New</a>

            <?php
            //add new
            if (isset($_POST) && $_POST && isset($_POST['views'])) {
                if (count($errorsBanner) > 0) {
                    foreach ($errorsBanner as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }
                } else {
                    echo '<div class="alert alert-success">Record Added Successfully.</div>';
                }
            }
            ?>

            <div class="addNewBanner">
                <form class="form-horizontal" role="form" method="post">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" id="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="views" class="col-sm-2 control-label">Views</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="views" id="hits">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cost" class="col-sm-2 control-label">Cost</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="cost" id="cost">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-primary">Add</button>
                        </div>
                    </div>
                </form>
            </div>

            <table class="table table-bordered">
                <thead><tr><th>#</th><th>Name </th><th>Impressions</th><th>Cost ($)</th><th>Action</th></tr></thead>
                <tbody>
                    <?php
                    $index = 1;
                    while ($row = mysql_fetch_array($adBanners)) :
                        ?>
                        <tr>
                            <td><?php echo $index; ?></td>
                            <td><?php $name = explode("banner_price_", $row['key']); echo $name[1]; ?></td>
                            <td><?php $value = explode("^", $row['value']); echo $value[0]; ?></td>
                            <td><?php echo $value[1]; ?></td>
                            <td>
                                <a href="advertising.php?action=deleteBanner&id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure you want to remove this record.');"><span class="glyphicon glyphicon-remove"></span></a>
                            </td>
                        </tr>
                        <?php
                        $index++;
                    endwhile;
                    ?>
                </tbody>
            </table>

          
        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

